package com.youyi.yynoticlibrary;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.internal.view.SupportMenu;
import android.widget.RemoteViews;
import com.youyi.yypermissionlibrary.core.os.SettingPage;
import java.util.HashMap;
import java.util.Map;

public class YYNoticSDK {
    private static final String YY_NOTIC_FLAG = "YY_NOTIC_FLAG";
    private static final YYNoticSDK ourInstance = new YYNoticSDK();
    Map<Integer, Object> ListenerMap = new HashMap();
    private Context mContext;
    private NotificationManager mManager;
    private MyReceive mMyReceive;
    Map<Integer, NoticBean> mRemoteViewsMap = new HashMap();

    public interface OnNoticClickListener {
        void click(int i);
    }

    public interface OnRemoteViewClickListener {
        void click(int i, int i2);
    }

    public static YYNoticSDK getInstance() {
        return ourInstance;
    }

    public void init(Context context) {
        this.mContext = context;
        registIntent();
    }

    public void destroy() {
        MyReceive myReceive;
        Context context = this.mContext;
        if (context != null && (myReceive = this.mMyReceive) != null) {
            context.unregisterReceiver(myReceive);
        }
    }

    private YYNoticSDK() {
    }

    private void registIntent() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(YY_NOTIC_FLAG);
        MyReceive myReceive = new MyReceive();
        this.mMyReceive = myReceive;
        this.mContext.registerReceiver(myReceive, intentFilter);
    }

    /* access modifiers changed from: private */
    public class MyReceive extends BroadcastReceiver {
        private MyReceive() {
        }

        public void onReceive(Context context, Intent intent) {
            OnRemoteViewClickListener onRemoteViewClickListener;
            if (intent.getAction().equals(YYNoticSDK.YY_NOTIC_FLAG)) {
                int intExtra = intent.getIntExtra("noticID", 0);
                Object obj = YYNoticSDK.this.ListenerMap.get(Integer.valueOf(intExtra));
                if (obj instanceof OnNoticClickListener) {
                    OnNoticClickListener onNoticClickListener = (OnNoticClickListener) obj;
                    if (onNoticClickListener != null) {
                        onNoticClickListener.click(intExtra);
                    }
                } else if ((obj instanceof OnRemoteViewClickListener) && (onRemoteViewClickListener = (OnRemoteViewClickListener) obj) != null) {
                    onRemoteViewClickListener.click(intExtra, intent.getIntExtra("viewID", 0));
                }
            }
        }
    }

    public NotificationManager getManager(Context context) {
        if (this.mManager == null) {
            this.mManager = (NotificationManager) context.getSystemService("notification");
        }
        return this.mManager;
    }

    public void normal(Context context, int i, String str, String str2, int i2, boolean z, boolean z2, OnNoticClickListener onNoticClickListener) {
        if (!hasNotic(context)) {
            openNotic(context);
            return;
        }
        this.ListenerMap.put(Integer.valueOf(i), onNoticClickListener);
        Intent intent = new Intent(YY_NOTIC_FLAG);
        intent.putExtra("noticID", i);
        PendingIntent broadcast = PendingIntent.getBroadcast(context, 0, intent, 201326592);
        if (Build.VERSION.SDK_INT >= 26) {
            NotificationManager manager = getManager(context);
            String str3 = context.getPackageName() + "id_" + i;
            NotificationChannel notificationChannel = new NotificationChannel(str3, context.getPackageName() + "name_" + i, 3);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(-65536);
            notificationChannel.setShowBadge(true);
            notificationChannel.setLockscreenVisibility(0);
            notificationChannel.setDescription("重要通知");
            notificationChannel.setSound(null, null);
            notificationChannel.enableVibration(false);
            NotificationManager manager2 = getManager(context);
            if (manager2 != null) {
                manager2.createNotificationChannel(notificationChannel);
            }
            Notification build = new NotificationCompat.Builder(context, str3).setContentTitle(str).setContentTitle(str).setContentText(str2).setWhen(System.currentTimeMillis()).setSmallIcon(i2).setContentIntent(broadcast).setAutoCancel(z).setVibrate(new long[]{0}).setSound(null).build();
            if (!z2) {
                build.flags |= 32;
            }
            manager.notify(i, build);
            return;
        }
        NotificationManager manager3 = getManager(context);
        Notification build2 = new Notification.Builder(context).setContentTitle(str).setContentText(str2).setWhen(System.currentTimeMillis()).setContentIntent(broadcast).setSmallIcon(i2).setAutoCancel(z).setVibrate(new long[]{0}).setSound(null).build();
        if (!z2) {
            build2.flags |= 32;
        }
        manager3.notify(i, build2);
    }

    public void view(Context context, int i, int i2, int i3, boolean z, boolean z2, OnRemoteViewClickListener onRemoteViewClickListener, int... iArr) {
        Notification notification;
        if (!hasNotic(context)) {
            openNotic(context);
            return;
        }
        this.ListenerMap.put(Integer.valueOf(i), onRemoteViewClickListener);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), i2);
        if (iArr != null && iArr.length > 0) {
            for (int i4 = 0; i4 < iArr.length; i4++) {
                Intent intent = new Intent(YY_NOTIC_FLAG);
                intent.putExtra("noticID", i);
                intent.putExtra("viewID", iArr[i4]);
                remoteViews.setOnClickPendingIntent(iArr[i4], PendingIntent.getBroadcast(context, i4 + i, intent, 201326592));
            }
        }
        if (Build.VERSION.SDK_INT >= 26) {
            NotificationManager manager = getManager(context);
            String str = context.getPackageName() + "id_" + i;
            NotificationChannel notificationChannel = new NotificationChannel(str, context.getPackageName() + "name_" + i, 3);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(-65536);
            notificationChannel.setShowBadge(true);
            notificationChannel.setLockscreenVisibility(0);
            notificationChannel.setDescription("重要通知");
            notificationChannel.setSound(null, null);
            notificationChannel.enableVibration(false);
            manager.createNotificationChannel(notificationChannel);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context, str);
            builder.setChannelId(str);
            notification = builder.setWhen(System.currentTimeMillis()).setContent(remoteViews).setSmallIcon(i3).setAutoCancel(z).setVibrate(new long[]{0}).setSound(null).build();
            if (!z2) {
                notification.flags |= 32;
            }
            manager.notify(i, notification);
        } else {
            NotificationManager manager2 = getManager(context);
            notification = new NotificationCompat.Builder(context).setWhen(System.currentTimeMillis()).setContent(remoteViews).setSmallIcon(i3).setAutoCancel(true).setVibrate(new long[]{0}).setSound(null).build();
            if (!z2) {
                notification.flags |= 32;
            }
            manager2.notify(i, notification);
        }
        this.mRemoteViewsMap.put(Integer.valueOf(i), new NoticBean(remoteViews, notification));
    }

    public void setTextViewText(int i, int i2, String str) {
        NoticBean noticBean = this.mRemoteViewsMap.get(Integer.valueOf(i));
        if (noticBean != null) {
            noticBean.getRemoteViews().setTextViewText(i2, str);
            getManager(this.mContext).notify(i, noticBean.getNotification());
        }
    }

    public void setImageViewResource(int i, int i2, int i3) {
        NoticBean noticBean = this.mRemoteViewsMap.get(Integer.valueOf(i));
        if (noticBean != null) {
            noticBean.getRemoteViews().setImageViewResource(i2, i3);
            getManager(this.mContext).notify(i, noticBean.getNotification());
        }
    }

    public void setImageViewBitmap(int i, int i2, Bitmap bitmap) {
        NoticBean noticBean = this.mRemoteViewsMap.get(Integer.valueOf(i));
        if (noticBean != null) {
            noticBean.getRemoteViews().setImageViewBitmap(i2, bitmap);
            getManager(this.mContext).notify(i, noticBean.getNotification());
        }
    }

    public void setImageViewUri(int i, int i2, Uri uri) {
        NoticBean noticBean = this.mRemoteViewsMap.get(Integer.valueOf(i));
        if (noticBean != null) {
            noticBean.getRemoteViews().setImageViewUri(i2, uri);
            getManager(this.mContext).notify(i, noticBean.getNotification());
        }
    }

    public void setViewVisibility(int i, int i2, int i3) {
        NoticBean noticBean = this.mRemoteViewsMap.get(Integer.valueOf(i));
        if (noticBean != null) {
            noticBean.getRemoteViews().setViewVisibility(i2, i3);
            getManager(this.mContext).notify(i, noticBean.getNotification());
        }
    }

    public void setTextColor(int i, int i2, int i3) {
        NoticBean noticBean = this.mRemoteViewsMap.get(Integer.valueOf(i));
        if (noticBean != null) {
            noticBean.getRemoteViews().setTextColor(i2, i3);
            getManager(this.mContext).notify(i, noticBean.getNotification());
        }
    }

    public void frontNormal(Service service, int i, String str, String str2, int i2, OnNoticClickListener onNoticClickListener) {
        if (!hasNotic(service)) {
            openNotic(service);
            return;
        }
        this.ListenerMap.put(Integer.valueOf(i), onNoticClickListener);
        Intent intent = new Intent(YY_NOTIC_FLAG);
        intent.putExtra("noticID", i);
        PendingIntent broadcast = PendingIntent.getBroadcast(service, 0, intent, 201326592);
        if (Build.VERSION.SDK_INT >= 26) {
            NotificationManager manager = getManager(service);
            String str3 = service.getPackageName() + "id_" + i;
            NotificationChannel notificationChannel = new NotificationChannel(str3, service.getPackageName() + "name_" + i, 4);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(-65536);
            notificationChannel.setShowBadge(true);
            notificationChannel.setLockscreenVisibility(0);
            notificationChannel.setDescription("重要通知");
            notificationChannel.setSound(null, null);
            notificationChannel.enableVibration(false);
            manager.createNotificationChannel(notificationChannel);
            service.startForeground(i, new NotificationCompat.Builder(service, str3).setContentTitle(str).setContentIntent(broadcast).setContentTitle(str).setContentText(str2).setSmallIcon(i2).setWhen(System.currentTimeMillis()).setAutoCancel(true).setVibrate(new long[]{0}).setSound(null).build());
            return;
        }
        service.startForeground(i, new Notification.Builder(service).setContentIntent(broadcast).setContentTitle(str).setContentText(str2).setSmallIcon(i2).setWhen(System.currentTimeMillis()).setAutoCancel(true).setVibrate(new long[]{0}).setSound(null).build());
    }

    public void frontView(Service service, int i, int i2, int i3, OnRemoteViewClickListener onRemoteViewClickListener, int... iArr) {
        Notification notification;
        if (!hasNotic(service)) {
            openNotic(service);
            return;
        }
        this.ListenerMap.put(Integer.valueOf(i), onRemoteViewClickListener);
        RemoteViews remoteViews = new RemoteViews(service.getPackageName(), i2);
        if (iArr != null && iArr.length > 0) {
            for (int i4 = 0; i4 < iArr.length; i4++) {
                Intent intent = new Intent(YY_NOTIC_FLAG);
                intent.putExtra("noticID", i);
                intent.putExtra("viewID", iArr[i4]);
                remoteViews.setOnClickPendingIntent(iArr[i4], PendingIntent.getBroadcast(service, i4 + i, intent, 201326592));
            }
        }
        if (Build.VERSION.SDK_INT >= 26) {
            NotificationManager manager = getManager(service);
            String str = service.getPackageName() + "id_" + i;
            NotificationChannel notificationChannel = new NotificationChannel(str, service.getPackageName() + "name_" + i, 4);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(-65536);
            notificationChannel.setShowBadge(true);
            notificationChannel.setLockscreenVisibility(0);
            notificationChannel.setDescription("重要通知");
            notificationChannel.setSound(null, null);
            notificationChannel.enableVibration(false);
            manager.createNotificationChannel(notificationChannel);
            notification = new NotificationCompat.Builder(service, str).setContent(remoteViews).setSmallIcon(i3).setWhen(System.currentTimeMillis()).setAutoCancel(true).setVibrate(new long[]{0}).setSound(null).build();
            service.startForeground(i, notification);
        } else {
            notification = new Notification.Builder(service).setContent(remoteViews).setSmallIcon(i3).setWhen(System.currentTimeMillis()).setAutoCancel(true).setVibrate(new long[]{0}).setSound(null).build();
            service.startForeground(i, notification);
        }
        this.mRemoteViewsMap.put(Integer.valueOf(i), new NoticBean(remoteViews, notification));
    }

    public void cancelFrontNotic(Service service) {
        service.stopForeground(true);
    }

    public void cancelNotic(Context context, int i) {
        getManager(context).cancel(i);
    }

    public void cancelAll(Context context) {
        getManager(context).cancelAll();
    }

    public boolean hasNotic(Context context) {
        try {
            return NotificationManagerCompat.from(context).areNotificationsEnabled();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public void openNotic(Context context) {
        try {
            Intent intent = new Intent();
            if (Build.VERSION.SDK_INT >= 26) {
                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                intent.putExtra("android.provider.extra.APP_PACKAGE", context.getPackageName());
            }
            if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT < 26) {
                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                intent.putExtra("app_package", context.getPackageName());
                intent.putExtra("app_uid", context.getApplicationInfo().uid);
            }
            if (Build.VERSION.SDK_INT < 21) {
                intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                intent.setData(Uri.fromParts(SettingPage.EXTRA_PKG, context.getPackageName(), null));
            }
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /* access modifiers changed from: private */
    public class NoticBean {
        private Notification mNotification;
        private RemoteViews mRemoteViews;

        public NoticBean(RemoteViews remoteViews, Notification notification) {
            this.mRemoteViews = remoteViews;
            this.mNotification = notification;
        }

        public RemoteViews getRemoteViews() {
            return this.mRemoteViews;
        }

        public void setRemoteViews(RemoteViews remoteViews) {
            this.mRemoteViews = remoteViews;
        }

        public Notification getNotification() {
            return this.mNotification;
        }

        public void setNotification(Notification notification) {
            this.mNotification = notification;
        }
    }
}
